package DAO.impl.hibernate;

import domain.Message;
import domain.Room;
import domain.User;

import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;

import utils.HibernateUtil;

public class DaoTest {

	private static Logger log = Logger.getLogger(RoomDAOTest.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		try {
			sessionFactory.getCurrentSession().beginTransaction();

			//testUserDao(sessionFactory);
			//testMessageDao(sessionFactory);
			//testRoomDao(sessionFactory);

			createTestData(sessionFactory);
			
			sessionFactory.getCurrentSession().getTransaction().commit();			
		} catch (Exception ex) {
			log.error("exception occured \n", ex);
			sessionFactory.getCurrentSession().getTransaction().rollback();
		}
	}

	private static void createTestData(SessionFactory sessionFactory) {		
		log.debug("createTestData Test started.");
		
		User user = new User();
		user.setUserAge(22);
		user.setUserGender(0);
		user.setUserInfo("user info");
		user.setUserNickName("user1");
		user.setUserPassword("12345");
		user.setUserStatus(0);

		UserDaoImpl userDao = new UserDaoImpl(sessionFactory);
		userDao.registerUser(user);

		Message message = new Message();
		message.setCreateDate(new Date());
		message.setMsg("message_1");
		message.setUser(user);

		Message message_2 = new Message();
		message_2.setCreateDate(new Date());
		message_2.setMsg("message_2");
		message_2.setUser(user);

		Message message_3 = new Message();
		message_3.setCreateDate(new Date());
		message_3.setMsg("message_3");
		message_3.setUser(user);

		Room room = new Room();
		room.setActive(Boolean.TRUE);
		room.setRoomDescription("room description 1");
		room.setRoomName("Room_1");
		room.setRoomRule("Rule_1");
		room.addMessage(message);
		room.addMessage(message_2);
		room.addMessage(message_3);
		
//		for (Message msg : room.getMessages()) {
//			log.debug("message is :" + msg.getId() + " " + msg.getMsg());
//		}

		RoomDaoImpl roomDao = new RoomDaoImpl(sessionFactory);

		log.debug("creating room ");
		roomDao.CreateRoom(room);
		log.debug("room created ");
		
		for (int i = 2; i < 7; i++) {
			
			room = new Room();
			room.setActive(Boolean.TRUE);
			room.setRoomDescription( "room description " + String.valueOf(i) );
			room.setRoomName("Room_" + String.valueOf(i) );
			room.setRoomRule("Rule_" + String.valueOf(i) );
			roomDao.CreateRoom(room);
			
		}
		
		
		log.debug("createTestData Test ended.");
	}

	private static void testRoomDao(SessionFactory sessionFactory) {
		log.debug("RoomDaoImpl Test started.");

		User user = new User();
		user.setUserAge(22);
		user.setUserGender(0);
		user.setUserInfo("user info");
		user.setUserNickName("user1");
		user.setUserPassword("12345");
		user.setUserStatus(0);

		UserDaoImpl userDao = new UserDaoImpl(sessionFactory);
		userDao.registerUser(user);

		Message message = new Message();
		message.setCreateDate(new Date());
		message.setMsg("message_1");
		message.setUser(user);

		Message message_2 = new Message();
		message_2.setCreateDate(new Date());
		message_2.setMsg("message_2");
		message_2.setUser(user);

		Message message_3 = new Message();
		message_3.setCreateDate(new Date());
		message_3.setMsg("message_3");
		message_3.setUser(user);

		Room room = new Room();
		room.setActive(Boolean.TRUE);
		room.setRoomDescription("room description");
		room.setRoomName("Room_1");
		room.setRoomRule("Rule_1");
		room.addMessage(message);
		room.addMessage(message_2);
		room.addMessage(message_3);
		
		for (Message msg : room.getMessages()) {
			log.debug("message is :" + msg.getId() + " " + msg.getMsg());
		}

		RoomDaoImpl roomDao = new RoomDaoImpl(sessionFactory);

		log.debug("creating room ");
		roomDao.CreateRoom(room);
		log.debug("room created ");

		Room bdRoom = roomDao.SelectRoom(1);
		log.debug("SelectRoom : " + bdRoom.getId() + " MSG_SIZE "
				+ bdRoom.getMessages().size() + " is active : "
				+ bdRoom.isActive());
			
		
		List<Room> rooms = roomDao.SelectRooms();
		for (Room tmp : rooms) {
			log.debug("SelectRooms : " + tmp.getId() + " MSG_SIZE "
			 + tmp.getMessages().size() );

			roomDao.DeleteRoom(tmp);
			log.debug("Room tmp deleted ");
		}
		
		log.debug("RoomDaoImpl Test completed.");
	}

	private static void testMessageDao(SessionFactory sessionFactory) {

		log.debug("MessagesDaoImpl Test started.");

		User user = new User();
		user.setUserAge(22);
		user.setUserGender(0);
		user.setUserInfo("user info");
		user.setUserNickName("user_2");
		user.setUserPassword("password");
		user.setUserStatus(0);

		UserDaoImpl userDao = new UserDaoImpl(sessionFactory);
		userDao.registerUser(user);
		
		Room room = new Room();
		room.setActive(true);
		room.setRoomDescription("room description");
		room.setRoomName("Room_2");
		room.setRoomRule("Rule_2");

		RoomDaoImpl roomDao = new RoomDaoImpl(sessionFactory);
		roomDao.CreateRoom(room);
		
		Message message = new Message();
		message.setCreateDate(new Date());
		message.setMsg("message_1");
		message.setRoom(room);
		message.setUser(user);

		Message message_2 = new Message();
		message_2.setCreateDate(new Date());
		message_2.setMsg("message_2");
		message_2.setRoom(room);
		message_2.setUser(user);
		
		MessagesDaoImpl messageDao = new MessagesDaoImpl(sessionFactory);

		log.debug("creating message_1 ");
		messageDao.createMessage(message);
		messageDao.createMessage(message_2);
		log.debug("messages created ");

		Message bdMessage = messageDao.getMessage(1);
		log.debug("getMessage : " + bdMessage.getCreateDate() + " "
				+ bdMessage.getMsg() + " USER_ID "
				+ bdMessage.getUser().getId() + " ROOM_ID "
				+ bdMessage.getRoom().getId());

		List<Message> messages = messageDao.getAllMessages();
		for (Message tmp : messages) {
			log.debug("getMessage : " + tmp.getCreateDate() + " "
					+ tmp.getMsg() + " USER_ID " + tmp.getUser().getId()
					+ " ROOM_ID " + tmp.getRoom().getId());

			int messageDeleted = messageDao.deleteMessage(tmp.getId());
			log.debug("Message tmp deleted "
					+ ((messageDeleted == 0) ? true : false));
		}

		log.debug("MessagesDaoImpl Test completed.");
	}

	private static void testUserDao(SessionFactory sessionFactory) {
		log.debug("UserDaoTest started.");
		User user = new User();
		user.setUserAge(22);
		user.setUserGender(0);
		user.setUserInfo("user info");
		user.setUserNickName("user_3");
		user.setUserPassword("qwerty");
		user.setUserStatus(0);

		User user_2 = new User();
		user_2.setUserAge(22);
		user_2.setUserGender(0);
		user_2.setUserInfo("user info 2");
		user_2.setUserNickName("user_2");
		user_2.setUserPassword("ytrewq");
		user_2.setUserStatus(0);
		
		UserDaoImpl userDao = new UserDaoImpl(sessionFactory);
		userDao.registerUser(user);
		userDao.registerUser(user_2);

		User bdUser = userDao.getUser(user.getUserNickName(),
				user.getUserPassword());
		log.debug("getUser : " + bdUser.getUserNickName() + " "
				+ bdUser.getUserPassword());

		List<User> users = userDao.getAllUsers();
		for (User tmp : users) {
			log.debug("getAllUsers : " + tmp.getId() + " "
					+ tmp.getUserNickName() + " " + tmp.getUserPassword());
			boolean userDeleted = userDao.deleteUser(tmp.getId());
			log.debug("User tmp deleted " + userDeleted);
		}
		log.debug("UserDaoTest completed.");
	}

}
